package com.we823.cms.controller.manage.ex;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.web.Oper;
import com.we823.cms.controller.manage.AbstractSimpleManageController;
import com.we823.cms.model.extension.Brand;
import com.we823.cms.repository.service.extension.BrandService;

@Controller
@RequestMapping(WebConstants.MANAGE_URL + "/product_brand")
public class ProductBrandManageController extends
		AbstractSimpleManageController<Brand, Integer> {

	private String baseUrl = WebConstants.MANAGE_URL + "/product_brand/";

	private BrandService brandService;

	@Autowired
	public ProductBrandManageController(BrandService brandService) {
		super(brandService);
		super.baseUrl = baseUrl;
		this.brandService = brandService;
	}

	@RequestMapping("changeStatus")
	@ResponseBody
	public Model changeStatus(HttpServletRequest request,Model model) {
		int id = RequestUtil.getValueByName(request, "id", 0);
		
		boolean hasError = false;
		String message = "";
		if (id>0) {
			int showIndex = RequestUtil.getValueByName(request, "showIndex", 0);
			try {
			   brandService.updateShowIndex(id,showIndex);
			   
			}catch(Exception ex) {
				hasError = true;
				message = "修改信息出错，id为"+id;
			}
		}else {
			hasError = true;
			message = "id小于0";
		}
		model.addAttribute("hasError",hasError);
		model.addAttribute("message",message);
		
		return model;
	}
	@Override
	public PageInfo<Brand> getData(PageRequest pageRequest,
								   HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<Brand> brandPage ;
		if (pageable) {
			brandPage = brandService.findAll(pageRequest);
		} else {
			brandPage = new PageInfo<>(
					brandService.findAll(getSort(request)));
		}
		return brandPage;
	}

	@Override
	public Brand getEntity(HttpServletRequest request, Integer id,
			Oper oper) {
		Brand brandModel = null;
		String name = RequestUtil.getValueByName(request, "name");
		String country = RequestUtil.getValueByName(request, "country");
		String homepage = RequestUtil.getValueByName(request, "homepage");
		int orderid = RequestUtil.getValueByName(request, "orderid", 0);
		String logo = RequestUtil.getValueByName(request, "logo", "NOLOGO.gif");
		if (Oper.ADD.equals(oper)) {
			brandModel = new Brand();
			brandModel.setName(name);
			brandModel.setCountry(country);
			brandModel.setHomepage(homepage);
			brandModel.setOrderid(orderid);
			brandModel.setAddtime(new Date());
			brandModel.setLogo(logo);
		} else {
			brandModel = brandService.findOne(id);
			if (brandModel != null) {

				brandModel.setId(id);
				brandModel.setName(name);
				brandModel = new Brand();
				brandModel.setName(name);
				brandModel.setCountry(country);
				brandModel.setHomepage(homepage);
				brandModel.setOrderid(orderid);
				brandModel.setEdittime(new Date());
				brandModel.setLogo(logo);
			} else {
				brandModel = new Brand();
				brandModel.setName(name);
				brandModel.setCountry(country);
				brandModel.setHomepage(homepage);
				brandModel.setOrderid(orderid);
				brandModel.setAddtime(new Date());
				brandModel.setEdittime(new Date());
				brandModel.setLogo(logo);
			}
		}
		return brandModel;
	}

	@Override
	public void setIndetity() {
		setResourceIdentity("ex:product_brand");
	}

}
